﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Cy.Windows.Forms.Windows.Forms
{
    public partial class CyDateBox : UserControl
    {
        private class ToolStripMonthCalendar : ToolStripControlHost
        {
            public ToolStripMonthCalendar() : base(new MonthCalendar())
            {
                this.Margin = Padding.Empty;
                this.Padding = Padding.Empty;
                this.AutoSize = false;
            }

            public MonthCalendar MonthCalendarControl
            {
                get
                {
                    return Control as MonthCalendar;
                }
            }

            // Expose the AddBoldedDate method.
            public void AddBoldedDate(DateTime dateToBold)
            {
                MonthCalendarControl.AddBoldedDate(dateToBold);
            }

        }

        private ToolStripMonthCalendar myTSMonthCalendar;
        private ToolStripDropDown dropdown;

        public CyDateBox()
        {
            InitializeComponent();
        }

        public DateTime? SeletedValue
        {
            get
            {
                string text = this.Text;
                int index = text.IndexOf("-");
                if (index != -1)
                    text = text.Substring(0, index);
                DateTime result;
                bool r = DateTime.TryParse(text, out result);
                if (r)
                    return result;

                return null;
            }
        }

        public new string Text
        {
            get { return textBox1.Text; }
            set
            {
                string text = value;
                int index = text.IndexOf("-");
                if (index != -1)
                    text = text.Substring(0, index);
                DateTime result;
                bool r = DateTime.TryParse(text, out result);
                if (r)
                    textBox1.Text = result.ToShortDateString();
                else
                    textBox1.Text = value;
            }
        }

        private void cyDropDownButton1_Click(object sender, EventArgs e)
        {
            try
            {
                OnDialogOpen(EventArgs.Empty);

                OpenDropDown();

                OnDialogClosed(EventArgs.Empty);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            SetContentAndButtonLocation(textBox1, cyDropDownButton1);
        }

        // 调整控件位置
        private void SetContentAndButtonLocation(Control content, Control rightButton)
        {
            Rectangle displayRectangle = DisplayRectangle;

            rightButton.Bounds = new Rectangle(displayRectangle.Right - 18,
                displayRectangle.Y,
                18,
                displayRectangle.Height);

            int width = rightButton.Location.X - DisplayRectangle.X;
            content.Bounds = new Rectangle(displayRectangle.Location, new Size(width, displayRectangle.Height));
        }

        #region ==== 预留接口 ====
        /// <summary>
        /// Fired when showing the drop down
        /// </summary>
        public event EventHandler DialogOpen;

        /// <summary>
        /// Fired when showing the drop down
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnDialogOpen(EventArgs e)
        {
            if (DialogOpen != null)
                DialogOpen(this, e);
        }

        /// <summary>
        /// Fired when closing the dropdown
        /// </summary>
        public event EventHandler DialogClosed;

        /// <summary>
        /// Fired when closing the dropdown
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnDialogClosed(EventArgs e)
        {
            if (DialogClosed != null)
                DialogClosed(this, e);
        }
        #endregion

        private void CyDateBox_Load(object sender, EventArgs e)
        {
            myTSMonthCalendar = new ToolStripMonthCalendar();
            dropdown = new ToolStripDropDown();
            // instantiere evenimente
            this.myTSMonthCalendar.MonthCalendarControl.DateSelected += MonthCalendarControl_DateSelected;
            this.myTSMonthCalendar.MonthCalendarControl.KeyDown += myTSMonthCalendar_KeyDown;
        }

        #region Control's Methods
        private void MonthCalendarControl_DateSelected(object sender, DateRangeEventArgs e)
        {
            this.Text = e.End.ToShortDateString();
            this.dropdown.Close();
        }
        private void myTSMonthCalendar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                MonthCalendarControl_DateSelected(sender, null);
            if (e.KeyCode == Keys.Escape)
            {
                this.dropdown.Close();
            }
        }
        #endregion

        private void OpenDropDown()
        {
            if (!this.dropdown.Visible)
            {
                if (this.SeletedValue != null)
                    this.myTSMonthCalendar.MonthCalendarControl.SetDate(SeletedValue.Value);

                dropdown.Items.Add(this.myTSMonthCalendar);
                Point point = new Point(0, this.Height);
                if ((this.PointToScreen(new Point(0, 0)).Y + Height + myTSMonthCalendar.Height) > Screen.PrimaryScreen.WorkingArea.Height)
                {
                    point.Y = -this.myTSMonthCalendar.Height - 7;
                }
                dropdown.Show(this, point);
            }
        }
    }
}
